<?php
class orderClassAction extends Action
{
    public function sendOrderAjax(){

        header('Content-Type: application/json; charset=utf-8');

        //请求url地址
        $appId = 'wxc2a0cb6fd674969a';
        $appSecret = 'af42fc23fa7be3f61270b7fee216cd45';
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
        //初始化curl
        $ch = curl_init($url);
        //3.设置参数
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        //4.调用接口
        $res = curl_exec($ch);
        if(curl_errno($ch)){
            var_dump(curl_error($ch));
        }
        $resArr = json_decode($res,1);
        //5.关闭curl
        curl_close($ch);

        $access_token = $resArr['access_token'];

        $Url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;   //模板信息请求地址

        $data = array(

            'first'=>array('value'=>"您好,有新的工单",'color'=>"#743A3A"),
            'keyword1'=>array('value'=>"狮子王公司",'color'=>'#FF0000'),
            'keyword2'=>array('value'=>"9-15 10:30",'color'=>'#FF0000'),
            'keyword3'=>array('value'=>"滨湖区科教园",'color'=>'#FF0000'),
            'keyword4'=>array('value'=>"电脑维修",'color'=>'#FF0000'),
            'keyword5'=>array('value'=>"麻烦准时",'color'=>'#FF0000'),
            'remark'=>array('value'=>'谢谢','color'=>'#FF0000'),
        );

        $template = array(
            'touser' => 'omgGn0hHKGo5CAK4hjeM_HmCM07g',
            'template_id' => 'EWTLiCj9_4JHTt9pjxjalnOCrFYvTC2nmcWdISAweNo',
            'url' => 'http://oa.wxlion.com/?d=we&m=wq&a=order',
            'topcolor' => 'red',
            'data' => $data
        );

        $post_data = json_encode($template);
        //发送数据，post方式<br>　　　　　　　　　//配置curl请求
        $ch = curl_init();//创建curl请求
        curl_setopt($ch, CURLOPT_URL,$Url); //设置发送数据的网址
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设置有返回值，0，直接显示
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); //禁用证书验证
        curl_setopt($ch, CURLOPT_POST, 1);//post方法请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//post请求发送的数据包
        //接收执行返回的数据
        $data = curl_exec($ch);
        //关闭句柄
        curl_close($ch);
        $data = json_decode($data,true); //将json数据转成数组

        echo '派单成功';
    }
}